import type { SiteListItem } from '@/apis/model/site'
import { deleteApiSiteApi, postApiSiteBatchPurgeApi } from '@/apis/modules/site/site'
import { useSiteStore } from '@/stores/site'

export default function useCommand() {
  const router = useRouter()
  const handleCommand = (
    command: string,
    ids: (string | number)[] | null,
    run: (() => void) | null,
    row?: SiteListItem
  ) => {
    const map: Record<string, () => void> = {
      delete: () => {
        // 站点删除
        CloudModal.error(
          {
            maskClosable: false,
            content: '该操作会删除此站点，确认删除？',
            okText: '删除'
          },
          async () => {
            await deleteApiSiteApi({ id: ids, purge_type: 2, })
            ToastUtils.success('删除成功')
            run!()
          }
        )
      },
      cache: () => {
        // 清除缓存
        CloudModal.error(
          {
            maskClosable: false,
            content: '该操作将永久清除所选站点的缓存，缓存将在数分钟后失效，确认继续？'
          },
          async () => {
            await postApiSiteBatchPurgeApi({ ids })
            ToastUtils.success('操作成功')
            run!()
          }
        )
      },
      config: () => {
        // 站点配置
        useSiteStore().setSiteInfo(row)
        router.push({
          name: 'SiteConfig',
          params: { id: row?.id }
        })
      }
    }
    map[command]()
  }
  return { handleCommand }
}
